package de.streblow.inventur;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Spinner;
import android.widget.TextView;

public class OptionsActivity extends Activity {

	private int indexScanAreaColor;
	private int intScanAreaWidth;
	private int intScanAreaHeight;
	private int intScanAreaEdgelet;
	private String textScanAreaWidth;
	private String textScanAreaHeight;
	private String textScanAreaEdgelet;
	private boolean bolPortraitDefault;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_options);
		SharedPreferences shared = getSharedPreferences("preferences", MODE_PRIVATE);
		indexScanAreaColor = shared.getInt("scanareacolor", 2);
		intScanAreaWidth = shared.getInt("scanareawidth", 95);
		intScanAreaHeight = shared.getInt("scanareaheight", 15);
		intScanAreaEdgelet = shared.getInt("scanareaedgelet", 10);
		textScanAreaWidth = getResources().getString(R.string.scanarea_width);
		textScanAreaHeight = getResources().getString(R.string.scanarea_height);
		textScanAreaEdgelet = getResources().getString(R.string.scanarea_edgelet);
		bolPortraitDefault = shared.getBoolean("portraitdefault", false);
		Spinner sp = (Spinner) findViewById(R.id.spinnerScanAreaColor);
		sp.setSelection(indexScanAreaColor);
		SeekBar sb = (SeekBar) findViewById(R.id.seekbarScanAreaWidth);
		sb.setProgress(intScanAreaWidth - 10);
		TextView tv = (TextView) findViewById(R.id.textScanAreaWidth);
		tv.setText(textScanAreaWidth + ": " + Integer.toString(intScanAreaWidth) + " %");
		sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				TextView tv = (TextView) findViewById(R.id.textScanAreaWidth);
				tv.setText(textScanAreaWidth + ": " + Integer.toString(progress + 10) + " %");
				intScanAreaWidth = progress + 10;
			}
			public void onStartTrackingTouch(SeekBar seekBar) {}
			public void onStopTrackingTouch(SeekBar seekBar) {}
		});
		sb = (SeekBar) findViewById(R.id.seekbarScanAreaHeight);
		sb.setProgress(intScanAreaHeight - 10);
		tv = (TextView) findViewById(R.id.textScanAreaHeight);
		tv.setText(textScanAreaHeight + ": " + Integer.toString(intScanAreaHeight) + " %");
		sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				TextView tv = (TextView) findViewById(R.id.textScanAreaHeight);
				tv.setText(textScanAreaHeight + ": " + Integer.toString(progress + 10) + " %");
				intScanAreaHeight = progress + 10;
			}
			public void onStartTrackingTouch(SeekBar seekBar) {}
			public void onStopTrackingTouch(SeekBar seekBar) {}
		});
		sb = (SeekBar) findViewById(R.id.seekbarScanAreaEdgelet);
		sb.setProgress(intScanAreaEdgelet);
		tv = (TextView) findViewById(R.id.textScanAreaEdgelet);
		tv.setText(textScanAreaEdgelet + ": " + Integer.toString(intScanAreaEdgelet) + " %");
		sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				TextView tv = (TextView) findViewById(R.id.textScanAreaEdgelet);
				tv.setText(textScanAreaEdgelet + ": " + Integer.toString(progress) + " %");
				intScanAreaEdgelet = progress;
			}
			public void onStartTrackingTouch(SeekBar seekBar) {}
			public void onStopTrackingTouch(SeekBar seekBar) {}
		});
		CheckBox cb = (CheckBox) findViewById(R.id.checkPortraitDefault);
		cb.setChecked(bolPortraitDefault);
		Button button1 = (Button) findViewById(R.id.buttonOk);
		button1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Spinner sp = (Spinner) findViewById(R.id.spinnerScanAreaColor);
				indexScanAreaColor = sp.getSelectedItemPosition();
				CheckBox cb = (CheckBox) findViewById(R.id.checkPortraitDefault);
				bolPortraitDefault = cb.isChecked();				
				SharedPreferences shared = getSharedPreferences("preferences", MODE_PRIVATE);
				SharedPreferences.Editor editor = shared.edit();
				editor.putInt("scanareacolor", indexScanAreaColor);
				editor.putInt("scanareawidth", intScanAreaWidth);
				editor.putInt("scanareaheight", intScanAreaHeight);
				editor.putInt("scanareaedgelet", intScanAreaEdgelet);
				editor.putBoolean("portraitdefault", bolPortraitDefault);
				editor.commit();
				finish();
			}
		});

		Button button2 = (Button) findViewById(R.id.buttonCancel);
		button2.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
	}
}
